﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace Sealight.UI.WebUI.UIHelpers
{
    public class FileObject
    {

        public static string SaveFile(HttpContext context, HttpPostedFileBase upload, Guid guid)
        {
            if (upload!= null)
            {
                string strFileName = Path.GetFileName(upload.FileName);
                string strExtension = Path.GetExtension(upload.FileName).ToLower();
                string strFilesLocation = context.Server.MapPath("~/Content/files").TrimEnd('\\');

                if (!Directory.Exists(strFilesLocation))
                {
                    Directory.CreateDirectory(strFilesLocation);
                }

                if (!string.IsNullOrEmpty(strExtension))
                {
                    string fileRaw = string.Format("{0}{1}", guid.ToString(), strExtension);

                    upload.SaveAs(string.Format("{0}\\{1}", strFilesLocation, fileRaw));
                    
                    return fileRaw;
                }
            }

            return string.Empty;

        }

        public static string SaveFile(HttpContext context, HttpPostedFileBase upload, Guid guid, string folder)
        {
            if (upload != null)
            {
                string strFileName = Path.GetFileName(upload.FileName);
                string strExtension = Path.GetExtension(upload.FileName).ToLower();
                string strFilesLocation = context.Server.MapPath("~/Content/files").TrimEnd('\\');
                string strUsersLocation = Path.Combine(strFilesLocation, folder);

                if (!Directory.Exists(strUsersLocation))
                {
                    Directory.CreateDirectory(strUsersLocation);
                }

                if (!string.IsNullOrEmpty(strExtension))
                {
                    string fileRaw = string.Format("{0}{1}", guid.ToString(), strExtension);

                    upload.SaveAs(string.Format("{0}\\{1}", strUsersLocation, fileRaw));                    

                    return fileRaw;
                }
            }

            return string.Empty;

        }
    }
}